namespace KinonekoSoftware.Studio.Pages.Galleries.Filters
{
    public sealed class RecentlyGalleryFilter : GalleryFilter
    {

        public override List<InstanceCache> Filter(ICollection<InstanceCache> collection, string searchBy)
        {
            List<InstanceCache> firstEpoch;
            int                 count;
            
            if (string.IsNullOrEmpty(searchBy))
            {
                firstEpoch = collection.OrderBy(x => x.TimeOfCreated)
                                       .ToList();
            }
            else
            {

                firstEpoch = collection.Where(x => x is not null                 &&
                                                   !string.IsNullOrEmpty(x.Name) &&
                                                   x.Name.Contains(searchBy))
                                       .OrderBy(x => x.TimeOfCreated)
                                       .ToList();

            }
            
            count = Math.Min(firstEpoch.Count, 20);
            return firstEpoch.Take(count).ToList();
        }

        public override string Name => "最近设定";
    }
}